package com.higgins.eventtracking.server.datamaintenance;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.jdo.Query;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.higgins.eventtracking.client.DietTrackingService;
import com.higgins.eventtracking.client.SecurityAccessDeniedException;
import com.higgins.eventtracking.client.datamaintenance.DataMaintenanceService;
import com.higgins.eventtracking.client.dto.AssetDTO;
import com.higgins.eventtracking.client.dto.FinanceGoalDTO;
import com.higgins.eventtracking.client.dto.HealthEventDTO;
import com.higgins.eventtracking.client.dto.InventoryDTO;
import com.higgins.eventtracking.client.dto.MealDTO;
import com.higgins.eventtracking.server.DateUtility;
import com.higgins.eventtracking.server.GenericDataStoreService;
import com.higgins.eventtracking.server.dataModel.Asset;
import com.higgins.eventtracking.server.dataModel.Consumption;
import com.higgins.eventtracking.server.dataModel.FinanceGoal;
import com.higgins.eventtracking.server.dataModel.HealthAlert;
import com.higgins.eventtracking.server.dataModel.HealthEvent;
import com.higgins.eventtracking.server.dataModel.Inventory;
import com.higgins.eventtracking.server.dataModel.InventoryAlert;
import com.higgins.eventtracking.server.dataModel.Meal;
import com.higgins.eventtracking.server.dataModel.Rule;
import com.higgins.eventtracking.server.dataModel.TrackingEvent;


public class DataMaintenanceDataService extends GenericDataStoreService implements DataMaintenanceService	{

	public Boolean insertMeal(String meal, String mealType, Double calories, Double protein, Double fat, Double water)	{
		pm.makePersistent(new Meal(meal, mealType, calories, protein, fat, water));
		return true;
	}
	public Boolean insertInventory(InventoryDTO dto)
			throws IllegalArgumentException {
		Inventory inventory = new Inventory();
		inventory.update(dto);
		pm.makePersistent(inventory);
		return true;
	}

	private List<HealthEvent> addHealthEventFilterAndExecute(Query query, String eventType)	{
		if (eventType.equals(""))	{
			return (List<HealthEvent>)query.execute();
		} else {
			query.setFilter("healthEventType == eventTypeParam");
			query.declareParameters("String eventTypeParam");
			return (List<HealthEvent>)query.execute(eventType);
		}
	}
	
	@Override
	public Integer retrieveNumberofHealthEvents(String eventType, Boolean clearNulls)
			throws IllegalArgumentException {
		Query query=pm.newQuery(HealthEvent.class);
		List<HealthEvent> healthEvents = addHealthEventFilterAndExecute(query, eventType);
		int count = 0;
		for (HealthEvent healthEvent:healthEvents)	{
			if (healthEvent.getHealthEventIntensityScale()==null)	{
				count++;
				if (clearNulls)	{
					setIntensityScale(healthEvent);
				}
			}
		}
		return count;
	}
	private void setIntensityScale(HealthEvent healthEvent)	{
		if (healthEvent.getHealthEventIntensity()==null)	{
			healthEvent.setHealthEventIntensityScale(8);
		} else if (healthEvent.getHealthEventIntensity().equals("Significant"))	{
			healthEvent.setHealthEventIntensityScale(10);
		} else if (healthEvent.getHealthEventIntensity().equals("Insignificant"))	{
			healthEvent.setHealthEventIntensityScale(0);
		} else if (healthEvent.getHealthEventIntensity().equals("Reasonable"))	{
			healthEvent.setHealthEventIntensityScale(5);
		}
		pm.makePersistent(healthEvent);
	}
	@Override
	public List<HealthEventDTO> listHealthEventDTOs(String eventType, Boolean clearNulls)
			throws IllegalArgumentException {
		Query query=pm.newQuery(HealthEvent.class);
		List<HealthEvent> healthEvents = addHealthEventFilterAndExecute(query, eventType);
		List<HealthEventDTO> healthEventDTOs = new ArrayList<HealthEventDTO>();
		for (HealthEvent healthEvent:healthEvents)	{
			healthEventDTOs.add(healthEvent.toDTO());
		}
		return healthEventDTOs;
	}
    @Override
    public Integer updateAllRules() throws IllegalArgumentException {
        Query query=pm.newQuery(Rule.class);
//        query.setFilter("parentDynamicType == null");
//        query.declareParameters("String eventTypeParam");
        List<Rule> rules = (List<Rule>)query.execute();

        int count = 0;
        for (Rule rule:rules)  {
            if (rule.getParentDynamicType()==null)   {
                count++;
                if (rule.getFrameOfMind().equals("House Keeping"))   {
                    rule.setParentDynamicType("FrameOfMindType");
                    rule.setTimestamp(new Date());
                    pm.makePersistent(rule);
                } else if (rule.getFrameOfMind().equals("Build For The Future"))   {
                    rule.setParentDynamicType("FrameOfMindType");
                    rule.setTimestamp(new Date());
                    pm.makePersistent(rule);
                } else if (rule.getFrameOfMind().equals("Legal Career"))   {
                    rule.setParentDynamicType("FrameOfMindType");
                    rule.setTimestamp(new Date());
                    pm.makePersistent(rule);
                } else if (rule.getFrameOfMind().equals("Current Career"))   {
                    rule.setParentDynamicType("FrameOfMindType");
                    rule.setTimestamp(new Date());
                    pm.makePersistent(rule);
                } else if (rule.getFrameOfMind().equals("Job"))   {
                    rule.setParentDynamicType("FrameOfMindType");
                    rule.setTimestamp(new Date());
                    pm.makePersistent(rule);
                } else if (rule.getFrameOfMind().equals("Knowledge"))   {
                    rule.setParentDynamicType("FrameOfMindType");
                    rule.setTimestamp(new Date());
                    pm.makePersistent(rule);
                } else if (rule.getFrameOfMind().equals("Relax"))   {
                    rule.setParentDynamicType("FrameOfMindType");
                    rule.setTimestamp(new Date());
                    pm.makePersistent(rule);
                } else if (rule.getFrameOfMind().equals("Relationship"))   {
                    rule.setParentDynamicType("FrameOfMindType");
                    rule.setTimestamp(new Date());
                    pm.makePersistent(rule);
                } else if (rule.getFrameOfMind().equals("Social"))   {
                    rule.setParentDynamicType("FrameOfMindType");
                    rule.setTimestamp(new Date());
                    pm.makePersistent(rule);
                } else if (rule.getFrameOfMind().equals("Anxious"))   {
                    rule.setParentDynamicType("FrameOfMindType");
                    rule.setTimestamp(new Date());
                    pm.makePersistent(rule);
                } else if (rule.getFrameOfMind().equals("Depressed"))   {
                    rule.setParentDynamicType("FrameOfMindType");
                    rule.setTimestamp(new Date());
                    pm.makePersistent(rule);
                    
                } else if (rule.getFrameOfMind().equals("Software"))   {
                    rule.setParentDynamicType("ExperienceType");
                    rule.setTimestamp(new Date());
                    pm.makePersistent(rule);
                } else if (rule.getFrameOfMind().equals("Software Skill"))   {
                    rule.setParentDynamicType("ExperienceType");
                    rule.setTimestamp(new Date());
                    pm.makePersistent(rule);
                } else if (rule.getFrameOfMind().equals("Finance"))   {
                    rule.setParentDynamicType("ExperienceType");
                    rule.setTimestamp(new Date());
                    pm.makePersistent(rule);
                } else if (rule.getFrameOfMind().equals("Commercial Awareness"))   {
                    rule.setParentDynamicType("ExperienceType");
                    rule.setTimestamp(new Date());
                    pm.makePersistent(rule);
                } else if (rule.getFrameOfMind().equals("Commerical Law"))   {
                    rule.setParentDynamicType("ExperienceType");
                    rule.setTimestamp(new Date());
                    pm.makePersistent(rule);

                } else if (rule.getFrameOfMind().equals("Breakfast"))   {
                    rule.setParentDynamicType("MealType");
                    rule.setTimestamp(new Date());
                    pm.makePersistent(rule);
                } else if (rule.getFrameOfMind().equals("Lunch"))   {
                    rule.setParentDynamicType("MealType");
                    rule.setTimestamp(new Date());
                    pm.makePersistent(rule);
                } else if (rule.getFrameOfMind().equals("Dinner"))   {
                    rule.setParentDynamicType("MealType");
                    rule.setTimestamp(new Date());
                    pm.makePersistent(rule);
                } else if (rule.getFrameOfMind().equals("Vegetables"))   {
                    rule.setParentDynamicType("MealType");
                    rule.setTimestamp(new Date());
                    pm.makePersistent(rule);
                } else if (rule.getFrameOfMind().equals("Take Away"))   {
                    rule.setParentDynamicType("MealType");
                    rule.setTimestamp(new Date());
                    pm.makePersistent(rule);
                } else {
                    rule.setParentDynamicType("SET THIS");
                    rule.setTimestamp(new Date());
                    pm.makePersistent(rule);
                }
            }
        }
        return count;
    }

}
